package com.server.model;

import com.common.Message;
import com.common.User;
import com.common.MessageType;
import com.server.controller.AcceptedSocketThreadManager;
import com.server.controller.ServerProcess;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Properties;

/**
 *
 */
/*
这是qq服务器,监听,等待某个qq客户端来连接
 */
public class ServerAccepter {
    private ServerSocket serverSocket;                          //服务端socket
    private ServerProcess serverProcess =  new ServerProcess(); //服务登陆端逻辑处理
    private boolean serverOpen;                                 //服务端开关标值

    public ServerAccepter() {
        Message message = null;                 //消息包
        ObjectOutputStream outputStream = null; //输出流
        serverOpen = true;
        try {
            //读取配置文件中的端口号供以套接字使用
            Properties properties = new Properties();
            properties.load(ServerAccepter.class.getClassLoader().getResourceAsStream("chat.properties"));

            //开启服务端的主机自动获取ip地址，并写入配置文件(不同主机写入不同的targetInterface)
            ScanInterface.fillList();
            String serverIp = ScanInterface.getIP();
            properties.setProperty("serverIp", serverIp);
            String serverPort = properties.getProperty("serverPort");
            serverSocket = new ServerSocket(Integer.parseInt(serverPort)); //构造一个服务端socket
            System.out.println("服务端:" + properties.getProperty("serverIp") + " 在端口【" + serverPort + "】监听");

            while (true) {
                //当标记值为false关闭服务端
                if (!serverOpen) {
                    AcceptedSocketThreadManager.removeAll(); //清空字典中用户信息内容
                    outputStream.close();                    //关闭输出流
                    serverSocket.close();                    //关闭服务端socket
                    break;
                }

                Socket socket = serverSocket.accept();       //阻塞等待用户连接
                ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream()); //输入流，接收客户发来的消息
                User user = (User)inputStream.readObject();  //存储读入信息

                //响应客户端
                message = new Message();
                outputStream = new ObjectOutputStream(socket.getOutputStream());

                //登陆成功
                if (serverProcess.login(user.getId(), user.getPwd())) {
                    System.out.println("用户ID:" + user.getId() + " 【" + user.getUsername() + "】登陆了");
                    //返回成功登陆消息包
                    message.setMesType(MessageType.message_login_success);
                    outputStream.writeObject(message);

                    AcceptedSocketThread acceptedSocketThread = new AcceptedSocketThread(socket); //开启一个线程服务于用户，保持通信
                    AcceptedSocketThreadManager.addClient(user.getId(), acceptedSocketThread);    //将用户id和对应的服务线程加入字典
                    acceptedSocketThread.start();
                }
                else {
                    //返回登陆失败消息包
                    System.out.println("验证失败，关闭此socket.");
                    message.setMesType(MessageType.message_login_fail);
                    outputStream.writeObject(message);
                    socket.close();
                }
            }
        } catch (Exception e) {
            System.out.println("Error" + e);
            e.printStackTrace();
        }
    }

    public void closeServer() {
        try {
            serverOpen = false;
            System.out.println("关闭服务端");
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
